<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" append-to-body @close="cancel">
    <el-row :gutter="48">
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree ref="tree" :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" default-expand-all @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
        <!-- 查询、筛选条件 -->
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-position="left" label-width="85px">
          <el-row :gutter="36">
            <el-col :xs="24" :sm="9">
              <el-form-item label="登录账号：" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入" clearable size="small" @keyup.enter.native="getList" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="9">
              <el-form-item label="手机号码：" prop="phonenumber">
                <el-input v-model="queryParams.phonenumber" placeholder="请输入" clearable size="small" @keyup.enter.native="getList" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="6">
              <span class="search-advancedButton">
                <el-button type="primary" size="small" @click="getList">查 询</el-button>
                <el-button size="small" @click="resetQuery">重 置</el-button>
              </span>
            </el-col>
          </el-row>
        </el-form>

        <!-- 选择用户提示框 -->
        <el-alert type="info" show-icon style="margin: 0 0 16px 0" :closable="false">
          <div slot="title">
            <span>已选择&nbsp;<strong>{{ ids.length }}</strong>&nbsp;项</span>
            <el-divider direction="vertical" />
            <span v-for="(item,key) in nickNames" :key="key">
              {{ item }}
            </span>
            <a v-if="ids.length > 0" @click="reset()">清空</a>
          </div>
        </el-alert>

        <!-- 表格 -->
        <el-table ref="userTable" v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <template slot="empty">
            <el-empty />
          </template>
          <el-table-column type="selection" width="80" fixed="left" align="center" />
          <el-table-column key="nickName" label="用户名" align="left" prop="nickName" :show-overflow-tooltip="true">
            <template slot-scope="{row}">
              <div class="table_nickName">
                <el-avatar style="margin-right: 8px" shape="circle" size="medium" :src="`/dev-api/${row.avatar}`" />
                {{ row.nickName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column key="userName" label="登录账号" align="left" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column key="phonenumber" label="手机号码" align="left" prop="phonenumber" />
          <el-table-column key="deptName" label="所属部门" align="center" prop="dept.deptName" />
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 确认 取消 按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
/**
 * 选择系统用户
 * @params
 * @params
 * by cuihaodong
 */
import { listUser } from '@/api/system/user'
import { treeselect } from '@/api/system/dept'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'ZrkSelectMultiUser',
  props: {
    multi: {
      type: Boolean,
      default: false
    },
    show: {
      type: Boolean,
      default: false
    },
    width: {
      type: [String, Number],
      default: '50%'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      visible: this.show,
      // 显示搜索条件
      showSearch: true,
      // 选中数组
      ids: [],
      nickNames: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined
      },
      // 总条数
      total: 0,
      title: '请选择指派人',
      // 部门树
      deptName: undefined,
      deptOptions: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 用户表格数据
      userList: null
    }
  },
  watch: {
    show: {
      immediate: true,
      handler(val) {
        this.visible = this.show
      }
    }
  },
  created() {
    this.getTreeselect()
    this.getList()
  },
  methods: {
    // 查询用户列表
    getList() {
      this.loading = true
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.userList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 查询部门下拉树结构
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId)
      this.nickNames = selection.map((item) => item.nickName)
    },
    // 提交按钮
    handleSubmit() {
      this.$emit('ok', this.ids, this.nickNames)
    },
    // 取消按钮
    cancel() {
      this.$emit('update:show', false)
      this.reset()
    },
    // 重置
    reset() {
      this.$refs.userTable.clearSelection()
      this.ids = []
      this.nickNames = []
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.getList()
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm')
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.table_nickName {
  margin: 1px 0 !important;
  display: flex;
  align-items: center;
}

::v-deep .el-table__expanded-cell {
  padding: 15px 115px !important;
}
</style>
